﻿using System;
using System.Data;
using System.Data.OleDb;

using Joel.ImpresionesJL.AccesoDatos.Interfaces;

namespace Joel.ImpresionesJL.AccesoDatos
{
    public class ServicioDatosCliente : ServicioDatosBase, IServicioDatosCliente
    {
        #region Constructores

        public ServicioDatosCliente()
        { }

        #endregion

        #region Metodos

        public DataSet Clientes_DevolverTodosHabilitados()
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_DevolverTodosHabilitados", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                OleDbDataAdapter da = new OleDbDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public DataSet Clientes_DevolverTodosEliminados()
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_DevolverTodosEliminados", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                OleDbDataAdapter da = new OleDbDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public DataSet Clientes_DevolverPorId(Int32 idCliente)
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_DevolverPorId", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new OleDbParameter("@idCliente", idCliente));
                OleDbDataAdapter da = new OleDbDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public Int32 Clientes_Insertar(String nombreFantasia, String razonSocial, String cuilCuit, String domicilio1,
            String domicilio2, String email1, String email2, String comentarios)
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_Insertar", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new OleDbParameter("@nombreFantasia", nombreFantasia));
                cm.Parameters.Add(new OleDbParameter("@razonSocial", razonSocial));
                cm.Parameters.Add(new OleDbParameter("@cuilCuit", cuilCuit));
                cm.Parameters.Add(new OleDbParameter("@domicilio1", domicilio1));
                cm.Parameters.Add(new OleDbParameter("@domicilio2", domicilio2));
                cm.Parameters.Add(new OleDbParameter("@email1", email1));
                cm.Parameters.Add(new OleDbParameter("@email2", email2));
                cm.Parameters.Add(new OleDbParameter("@comentarios", comentarios));
                cm.Connection.Open();
                return Convert.ToInt32(cm.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public void Clientes_Modificar(Int32 idCliente, String nombreFantasia, String razonSocial, String cuilCuit,
            String domicilio1, String domicilio2, String email1, String email2, String comentarios)
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_Modificar", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new OleDbParameter("@idCliente", idCliente));
                cm.Parameters.Add(new OleDbParameter("@nombreFantasia", nombreFantasia));
                cm.Parameters.Add(new OleDbParameter("@razonSocial", razonSocial));
                cm.Parameters.Add(new OleDbParameter("@cuilCuit", cuilCuit));
                cm.Parameters.Add(new OleDbParameter("@domicilio1", domicilio1));
                cm.Parameters.Add(new OleDbParameter("@domicilio2", domicilio2));
                cm.Parameters.Add(new OleDbParameter("@email1", email1));
                cm.Parameters.Add(new OleDbParameter("@email2", email2));
                cm.Parameters.Add(new OleDbParameter("@comentarios", comentarios));
                cm.Connection.Open();
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public void Clientes_Habilitar(Int32 idCliente)
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_Habilitar", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Connection.Open();
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public void Clientes_Eliminar(Int32 idCliente)
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_Eliminar", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Connection.Open();
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        public DataSet Clientes_ArmarCuentaCorriente(Int32 idCliente)
        {
            try
            {
                OleDbCommand cm = new OleDbCommand("Clientes_ArmarCuentaCorriente", Conexion);
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add(new OleDbParameter("@idCliente", idCliente));
                OleDbDataAdapter da = new OleDbDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Conexion.State == ConnectionState.Open)
                    Conexion.Close();
            }
        }

        #endregion
    }
}
